import {
	TokenKey
} from "../vars/StoreKeys.js"
const Http = function(url, filePath, name = 'file', formData = {}, header = {}, config = {}) {
	console.log(url)
	return new Promise((resolve, reject) => {
		const baseUrl = 'https://api.nnlxgj.com/api';
		if (url.startsWith('/')) {
			url = `${baseUrl}${url}`
		} else {
			url = `${baseUrl}/${url}`
		}
		try {
			const token = uni.getStorageSync(TokenKey)
			if (token && token.length > 0) {
				header.Authorization = 'Bearer ' + token
			}
		} catch (e) {

		}
		uni.uploadFile({
			url,
			filePath,
			name,
			header,
			formData,
			...config,
			success: (res) => {
				if (res.statusCode !== 200) {
					return reject(res.errMsg)
				}
				let data = res.data || {}
				if (typeof data === 'string') {
					try {
						data = JSON.parse(data)
					} catch (e) {

					}
				}
				if (data.code !== 200) {
					return reject(data.message || '')
				}
				resolve(data.data)
			},
			fail: (err) => {
				reject(err)
			}
		});
	})
}

export default Http;
